package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.MessageCheckInLogic;

import data.MessageCheckIn;

@SuppressWarnings("serial")
public class MessageCheckInService extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			String doClass = req.getParameter("func");
			if (doClass.equals("addMessageCheckIn")) {
				AddMessageCheckIn(req, resp);
			}else if(doClass.equals("editMessageCheckIn")){
				EditMessageCheckIn(req, resp);
			}else if(doClass.equals("getMessageCheckIn")){
				GetMessageCheckIn(req, resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void AddMessageCheckIn(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException, ParseException {
		HttpSession session = req.getSession(true);
		long hotelId = (Long) session.getAttribute("hotelId");
		String txtMessage = (String) req.getParameter("txtMessage");
		String thxMessage = (String) req.getParameter("thxMessage");
		String cardbody = (String) req.getParameter("cardbody");
		boolean ispublish = Boolean.parseBoolean(req.getParameter("ispublish"));
		Object obj = new MessageCheckInLogic().AddMessageCheckIn(hotelId, txtMessage,
				thxMessage,cardbody,ispublish);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void EditMessageCheckIn(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException, ParseException {
		long id = Long.parseLong(req.getParameter("id"));
		String txtMessage = (String) req.getParameter("txtMessage");
		String thxMessage = (String) req.getParameter("thxMessage");
		String cardbody = (String) req.getParameter("cardbody");
		boolean ispublish = Boolean.parseBoolean(req.getParameter("ispublish"));
		Object obj = new MessageCheckInLogic().EditMessageCheckIn(id, txtMessage,
				thxMessage,cardbody,ispublish);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetMessageCheckIn(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException, ParseException {
		HttpSession session = req.getSession(true);
		long hotelId = (Long) session.getAttribute("hotelId");
		List<MessageCheckIn> obj1 = new MessageCheckInLogic().GetMessageCheckInAll(hotelId);
		MessageCheckIn obj = obj1.get(0);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
}
